﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Model;
using DispatchAssist.Service;
using DispatchAssist.BusinessLogical.Model;
using System.Threading;

namespace DispatchAssist.AppHost
{
    public partial class frmPromptAlert : Form
    {
        public List<NormalAlertInfo> alertinfoLst;
        AlertMessageGetterService service = new AlertMessageGetterService();
        delegate void FunDelegate();

        Sunisoft.IrisSkin.SkinEngine skinEngine1;
        private readonly object locker = new object();

        public frmPromptAlert()
        {
            InitializeComponent();

            skinEngine1 = SkinConfig.Instance.EnsureSkin(this);
        }
        //delegate void www();
        //delegate void hhh(object obj);

        private void InsertUserControls()
        {
            int i = alertinfoLst.Count;
            try
            {
                int hh = 0;
                //foreach (var item in alertinfoLst)
                //{
                //    //if (i == alertinfoLst.Count) this.panelPrompt.Controls.Add(new ucPromptAlert(item, i--) { Dock = DockStyle.Bottom });
                //    this.panelPrompt.Controls.Add(new ucPromptAlert(item, i--) { Dock = DockStyle.Top });
                //    //if (i == 200) break;
                //}


                foreach (var item in alertinfoLst)
                {
                    //if (i == alertinfoLst.Count) this.panelPrompt.Controls.Add(new ucPromptAlert(item, i--) { Dock = DockStyle.Bottom });
                    this.panelPrompt.Controls.Add(new ucPromptAlert(item, i--) { Dock = DockStyle.Top });
                    //if (i == 200) break;
                }


                //for (int ii = 500; ii >= 0; ii--)
                //{
                //    //if (i == alertinfoLst.Count) this.panelPrompt.Controls.Add(new ucPromptAlert(item, i--) { Dock = DockStyle.Bottom });
                //    this.panelPrompt.Controls.Add(new ucPromptAlert(new NormalAlertInfo(), ii--) { Dock = DockStyle.Top });
                //    //if (i == 200) break;
                //}

                ////this.panelPrompt.AutoScrollPosition = new Point(0, 0);
                //for (int di = 0; di < alertinfoLst.Count; di++)
                //{
                //    hh += this.panelPrompt.Controls[di].Height;
                //}
                //////this.panelPrompt.Controls[alertinfoLst.Count - 1].Dock = DockStyle.Fill;
                //this.panelPrompt.Height = hh + 1000;
                ////string str = this.panelPrompt.Height.ToString() + "======" + hh.ToString();
            }
            catch (Exception ex)
            {
                throw ex;

            }

        }

        private void addPromptAlertControls()
        {
            //www dd = new www(sss);
            //dd.Invoke();
            InsertUserControls();
        }

        public frmPromptAlert(List<NormalAlertInfo> infolst)
            : this()
        {
            alertinfoLst = infolst;
            //wjs();
        }

        private void showp(object obj)
        {

            frmPromptAlert frm = (frmPromptAlert)obj;

            AlertContext.Dic_Prompt_List.Add(frm);
            //if (frm.InvokeRequired)
            //{
            //    //frm.Show();
            //    MethodInvoker invoker = new MethodInvoker(frm.Show);
            //    //frm.Invoke();
            //    frm.Invoke(invoker, null);
            //}
            //else
            //{
            //    frm.Show();

            //}
            //frm.Activate();
        }

        private void callbackm(IAsyncResult result)
        {
            lock (locker)
            {
                if (result.IsCompleted)
                {
                    //hhh dddd = showp;
                    //MessageBox.Show("ok");
                    //dddd.Invoke(result.AsyncState);
                    AlertContext.Dic_Prompt_List.Add(((frmPromptAlert)result.AsyncState));

                    //((frmPromptAlert)result.AsyncState).Enabled = true;
                }
            }
        }

        public void wjs()
        {

            FunDelegate funDelegate = new FunDelegate(addPromptAlertControls);

            /*========================================================
             * 使用this.BeginInvoke方法
             * （也可以使用this.Invoke()方法）
            ========================================================*/

            // this.BeginInvoke(被调用的方法的委托，要传递的参数[Object数组])


            IAsyncResult aResult = funDelegate.BeginInvoke(callbackm, this);


            // 用于等待异步操作完成(-1表示无限期等待)
            //aResult.AsyncWaitHandle.WaitOne(0);

            //// 使用this.EndInvoke方法获得返回值
            //string str = (string)this.EndInvoke(aResult);
            //MessageBox.Show(str.ToString());
            //addPromptAlertControls();

        }

        private void frmPromptAlert_Load(object sender, EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            service.UpdateAlertStatusCompleted += new UpdateAlertStatusCompletedEventHandler(service_UpdateAlertStatusCompleted);
            this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel);

            //panelPrompt.AutoScrollMinSize = new Size(20, 30);

        }


        void FormSample_MouseWheel(object sender, MouseEventArgs e)
        {
            //获取光标位置
            Point mousePoint = new Point(e.X, e.Y);
            //换算成相对本窗体的位置
            mousePoint.Offset(this.Location.X, this.Location.Y);
            //判断是否在panel内
            if (panelPrompt.RectangleToScreen(panelPrompt.DisplayRectangle).Contains(mousePoint))
            {
                //滚动
                panelPrompt.AutoScrollPosition = new Point(0, panelPrompt.VerticalScroll.Value - e.Delta);
            }
        }
        private string[] GetStringArrByLst()
        {
            string[] strarr = new string[alertinfoLst.Count];

            for (int i = 0; i < strarr.Length; i++)
            {
                strarr[i] = alertinfoLst[i].AlertID.ToString();
            }
            return strarr;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string[] strarr = GetStringArrByLst();

            AlertContext.Dic_Dispose_Prompt.Add(alertinfoLst[0].WarnMsg, this);
            service.UpdateAlertStatusAsync(strarr, (int)(Enums.AlertActionFlag.PromptAlert), service.DASoapHeaderValue.UserId, alertinfoLst[0].WarnMsg);

            this.Close();
        }

        void service_UpdateAlertStatusCompleted(object sender, AsyncCompletedEventArgs e)
        {
            lock (locker)
            {
                AlertContext.Dic_Prompt.Remove((string)e.UserState);
                AlertContext.Dic_Dispose_Prompt.Remove((string)e.UserState);
            }
        }


    }
}